package org.sxf.app.sign.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextClock;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.sxf.app.sign.R;
import org.sxf.app.sign.adapter.SignHistoryListAdapter;
import org.sxf.app.sign.biz.BaseBiz;
import org.sxf.app.sign.biz.SignBiz;
import org.sxf.app.sign.data.DataCacheSingleton;
import org.sxf.app.sign.model.SignHistoryBean;
import org.sxf.app.sign.model.SignRepBean;
import org.sxf.app.sign.provider.LocProviderSingleton;
import org.sxf.app.sign.utils.TimeUtils;
import org.sxf.app.sign.widgt.T;

public class SignActivity extends BaseActivity implements BaseBiz.OnBizCallBackListener{


    private SignBiz mSignBiz;
    private SignHistoryListAdapter mSignHistoryListAdapter;

    @ViewInject(R.id.signButton)
    RelativeLayout mSignButton;

    @ViewInject(R.id.id_listview)
    ListView mSignHistoryListView;


    @OnClick(R.id.signButton)
    public void onClickReSelectXq(View view){
        mSignBiz.signApp();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);

        ViewUtils.inject(this);

        initUI();
        initBiz();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocProviderSingleton.getInstance().stop();
    }

    private void initUI(){

        mSignHistoryListAdapter = new SignHistoryListAdapter(this.getLayoutInflater());
        mSignHistoryListView.setAdapter(mSignHistoryListAdapter);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
            TextClock textClock = new TextClock(this);
            textClock.setFormat24Hour("HH:mm");
            textClock.setTextColor(getResources().getColor(R.color.sign_btn_time));
            RelativeLayout.LayoutParams cellParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            cellParams.addRule(RelativeLayout.ABOVE, R.id.signButtonTitle);
            cellParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
            cellParams.addRule(RelativeLayout.ALIGN_BOTTOM, getResources().getDimensionPixelSize(R.dimen.sign_clock_margin_bottom));
            mSignButton.addView(textClock);
            textClock.setLayoutParams(cellParams);
        }else {

        }
    }

    private void initBiz(){
        LocProviderSingleton.getInstance().start();
        mSignBiz = new SignBiz(this, this);
    }


    @Override
    public void onBizResult(int biz, Object result) {
        switch (biz){
            case SignBiz.BIZ_SIGN_FAIL:
                T.s(this, getString(R.string.sign_fail) + result);
                break;
            case SignBiz.BIZ_SIGN_SUCCESS:
                SignRepBean bean = (SignRepBean)result;
//                T.s(this, getString(R.string.sign_success) + ":" + bean.corp_name + "," + TimeUtils.formatDataTimeOnlyForTime(bean.srv_time));
                SignHistoryBean shb = new SignHistoryBean();
                shb.corpName = bean.corp_name;
                shb.signTime = TimeUtils.formatDataTimeOnlyForTime(bean.srv_time);
//                shb.signTime = bean.srv_time;
                DataCacheSingleton.getInstance().addSignToHistory(shb);
                mSignHistoryListAdapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public void onBizError(int biz, Object result) {
        switch (biz){
            case SignBiz.ERROR_SIGN_NET_ERROR:
                T.s(this, getString(R.string.net_error));
                break;
        }
    }
}
